home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9141 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  53 lines

  1. Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1
  2. Path: netcom.com!wallace
  3. From: wallace@netcom.com (David E. Wallace)
  4. Subject: Re: GOTO controversy
  5. Message-ID: <wallaceDnvoEo.7L7@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <rcshlds.1.000A6705@mailserv.mta.ca> <Dn8pJ8.nqs@emi.net> <4grt4e$8fg@goanna.cs.rmit.EDU.AU> <4hl8mt$4po@newshost.cyberramp.net>
  8. Date: Thu, 7 Mar 1996 03:30:24 GMT
  9. Sender: wallace@netcom7.netcom.com
  10.  
  11. In article <4hl8mt$4po@newshost.cyberramp.net>,
  12. John Noland <sinan@cyberramp.net> wrote:
  13. >A lot of programmers hate goto with fanatical fervor. I personally feel they 
  14. >can make for better code in certain situations. The first situation would
  15. >be to eliminate multiple return statements. A goto branch to a SINGLE label
  16. >is greatly preferable to multiple return statements with several exit points.
  17. >A subroutine with a single exit point is a lot easier to maintain than one 
  18. >with several exit points.
  19.  
  20. Ok, I'll bite: why is a subroutine with multiple gotos to a single exit point
  21. a "lot" easier to maintain than that same routine with multiple returns?
  22.  
  23. It isn't any easier to understand: the meaning of an early return
  24. due to an error is well understood and controlled, just as the meaning of
  25. "break" or "continue" is well understood with respect to loop termination.
  26. In contrast, the meaning of "goto foobarexit" requires scanning ahead
  27. to the definition of foobarexit to determine exactly what happens next,
  28. even if you can guess from the label that this is some sort of exit
  29. processing.  If you want to know if a given routine has multiple exit
  30. conditions, it's just as easy to scan backwards for "return" as it
  31. is to scan for "goto foobarexit."  The only maintenance tasks that I
  32. can see that get any easier are inserting special-purpose cleanup
  33. code that must run before a given function returns (regardless of
  34. why it is returning), or changing the return type of a function to
  35. a type that existing return values won't get automatically coerced to.
  36.  
  37. I can't see that either of these comes up often enough to warrant
  38. claiming great advantages for the "single goto" solution.  In the case of
  39. the cleanup code, it seems more likely that the type of cleanup will depend
  40. on the reason for termination (as it did in the original example, where
  41. you thought the goto solution to multiple labels was inappropriate).
  42.  
  43. The "single goto" solution isn't horrible; I just don't see the great
  44. advantages claimed for it.  On balance, I prefer multiple returns
  45. when a simple return is appropriate, just because it is slightly more
  46. self-documenting.  I have no problems with a goto when complicated
  47. cleanup must be done, but I find this case to be rare.
  48.  
  49. So what maintenance issues come up often enough in your environment that
  50. you find the "single goto" solution far superior to multiple returns?
  51. -- 
  52. Dave Wallace        (wallace@netcom.com)
  53.